package com.xframe.domain.sys;

import com.xframe.domain.BaseModel;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.xframe.xdal.core.annotation.AutoFill;
import com.xframe.xdal.core.annotation.XColumn;
import com.xframe.xdal.core.annotation.XSelectItem;
import com.xframe.xdal.core.annotation.XTable;
import com.xframe.xdal.core.constant.AutoFillType;
import com.xframe.xdal.core.model.mapping.IRelation;
import com.xframe.xdal.core.model.mapping.ITableInfo;
import com.xframe.xdal.core.model.mapping.TableInfoBuilder;
import lombok.Data;

/**
 * 系统实体子表
 *
 * @author xufeng
 * @version 1.0
 * @date 2023-03-28
 */
@Data
@XTable(value = "SYS_MODEL_MANY", label = "模型子表信息")
@XSelectItem(keyName = "manyId",codeName = "manyId",textName = "refName")
public class SysModelMany extends BaseModel implements IRelation {
    /**
     * 字段处理器ID
     */
    @AutoFill(AutoFillType.Snowflake)
    @JsonSerialize(using = com.fasterxml.jackson.databind.ser.std.ToStringSerializer.class)
    @XColumn(name = "MODEL_MANY_ID", primaryKeyFlag = true,desc = "字段处理器ID")
    private Long manyId;

    /**
     * 模型ID
     */
    @XColumn(name = "MODEL_ID",uniqueFlag = true,desc = "模型ID")
    @JsonSerialize(using = com.fasterxml.jackson.databind.ser.std.ToStringSerializer.class)
    private Long modelId;

    /**
     * 子表名称
     */
    @XColumn(name = "MANY_AS_NAME",uniqueFlag = true,desc = "子表名称")
    private String asName;


    /**
     * 模型目标模型ID
     */
    @XColumn(name = "TAR_MODEL_ID",desc = "模型目标模型ID")
    private Long tarModelId;

    /**
     * 目标模型Code
     */
    @XColumn(name = "TAR_MODEL_CODE",desc = "目标模型Code")
    private String tarModelCode;

    /**
     * 对象的类型
     */
    @XColumn(name = "CLASS_NAME",desc = "对象的类型")
    private String className;

    /**
     * 附加字符串
     */
    @XColumn(name = "REF_STR",desc = "附加字符串")
    private String refStr;

    /**
     * 外键名称
     */
    @XColumn(name = "F_KEY",desc = "外键名称")
    @JsonProperty("fKey")
    private String fKey;

    /**
     * 目标表键名称
     */
    @XColumn(name = "TAR_KEY",desc = "目标表键名称")
    private String tarKey;

    /**
     * 子表的被填充属性名称
     */
    @XColumn(name = "REF_NAME",nullFlag = true,desc = "子表的被填充属性名称")
    private String refName;


    @Override
    public String getTypeName() {
        return this.className;
    }

    @Override
    public String getForeignKey() {
        return this.fKey;
    }

    @Override
    public String getTargetKey() {
        return this.tarKey;
    }

    @Override
    public String getAs() {
        return this.asName;
    }

    @Override
    public String getRefTableAddString() {
        return this.refStr;
    }

    @Override
    public String getRefName() {
        return this.refName;
    }

    private Class<?> dataType = null;
    private ITableInfo refTableInfo = null;

    @JsonIgnore
    @Override
    public Class<?> getDataType() {
        if(dataType == null){
            try{
                dataType = Class.forName(this.className);
            }catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return dataType;
    }

    @JsonIgnore
    @Override
    public ITableInfo getTableInfoByType() {
        if(refTableInfo == null){
            refTableInfo = TableInfoBuilder.BuildTableInfoByAnnotation(getDataType());
        }
        return refTableInfo;
    }
}
